﻿using MediatR;
using ProcurementManagement.Applcation.Command.ProcureApplyCommand;
using ProcurementManagement.Infrstructre.Interface;

namespace ProcurementManagement.Applcation.CommandHanlers.ProcureApplyCommandHandler
{
    /// <summary>
    /// 删除采购申请中介者
    /// </summary>
    public class ProcureApplyDelCommandHandler:IRequestHandler<ProcureApplyDelCommand,int>
    {
        private readonly lnterfaceProcureApplyRepository _procureApplyRepository;//采购申请仓储接口

        public ProcureApplyDelCommandHandler(lnterfaceProcureApplyRepository procureApplyRepository)
        {
            _procureApplyRepository = procureApplyRepository;
        }

        public async Task<int> Handle(ProcureApplyDelCommand request, CancellationToken cancellationToken)
        {
            var count = 0;
            foreach (var item in request.id)
            {
                var res = (await _procureApplyRepository.GetAll()).First(x => x.ProcureApplyId == item);
                if (res != null)
                {
                    res.IsDel = true;
                    res.DelName = request.DelName;
                    res.DelTime = DateTime.Now;
                    await _procureApplyRepository.UpdataAsync(res);
                    count = 1;
                }
                else
                {
                    count = -1;//不存在
                }
            }
            return count;
        }
    }
}
